import json

from flask import abort, Blueprint, g, session, jsonify

from .database import db
from .models import CourseState
from .crud import Users, Timetables, Attendances
from .schemas import timetable_schema

bp = Blueprint('data', __name__, url_prefix='/data')

@bp.before_app_request
def load_logged_in_user():
    user_id = session.get('user_id')
    if user_id is None:
        g.user = None
    else:
        g.user = Users.get_user(db.session, user_id)

@bp.route('/lesson/<id>')
def get_lesson(id):
    if g.user is None:
        return abort(401)

    timetable = Timetables.get_timetable(db.session, id)
    if timetable is None:
        return abort(404)
    return timetable_schema.dump(timetable)

@bp.route('/lessons/week')
def get_lessons_of_week(id):
    if g.user is None:
        return abort(401)

    timetable = Timetables.get_timetables_of_week(db.session, id)
    if timetable is None:
        return abort(404)
    return timetable_schema.dump(timetable)

@bp.route('/lesson/<id>/signin', methods=['PUT'])
def signin_lesson(id):
    print("-signin_lesson--", id)
    if g.user is None:
        return abort(401)

    timetable = Timetables.get_timetable(db.session, id)
    if timetable is None:
        return abort(404)
    if timetable.state != CourseState.signing_in and timetable.state != CourseState.started:
        return abort(400)
    if not Attendances.has(db.session, timetable.id, g.user.id):
        Attendances.add(db.session, timetable.id, g.user.id)
    return jsonify(success=True)

@bp.route('/lesson/<id>/open', methods=['PUT'])
def open_lesson(id):
    if g.user is None:
        return abort(401)

    timetable = Timetables.get_timetable(db.session, id)
    if timetable is None:
        return abort(404)
    if timetable.state != CourseState.not_started:
        return abort(400)
    if g.user.id != timetable.course.professor_id:
        return abort(403)
    timetable = Timetables.open_lesson(db.session, id)
    return timetable_schema.dump(timetable)

@bp.route('/lesson/<id>/start', methods=['PUT'])
def start_lesson(id):
    if g.user is None:
        return abort(401)

    timetable = Timetables.get_timetable(db.session, id)
    if timetable is None:
        return abort(404)
    if timetable.state != CourseState.signing_in:
        return abort(400)
    if g.user.id != timetable.course.professor_id:
        return abort(403)
    timetable = Timetables.start_lesson(db.session, id)
    return timetable_schema.dump(timetable)

@bp.route('/lesson/<id>/end', methods=['PUT'])
def end_lesson(id):
    if g.user is None:
        return abort(401)

    timetable = Timetables.get_timetable(db.session, id)
    if timetable is None:
        return abort(404)
    if timetable.state != CourseState.started:
        return abort(400)
    if g.user.id != timetable.course.professor_id:
        return abort(403)
    timetable = Timetables.end_lesson(db.session, id)
    return timetable_schema.dump(timetable)
